import 'package:cbec_app/components/base/base_ctr.dart';
import 'package:cbec_app/components/http/network.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';

import '../../../utils/EventMgr.dart';

///我的预售订单控制器
class PresaleOrderController extends BaseController with SingleGetTickerProviderMixin {
  late TabController tabController;

  final TextEditingController searchTxt = TextEditingController();

  String keyWords = '';

  int page = 1;

  int type = 0;

  bool hasNextPage = false;

  ///订单列表
  List orderList = [];

  @override
  void onInit() {
    getMyorderLists();
    tabController = TabController(length: 6, vsync: this);
    tabController.addListener(() {
      if (tabController.indexIsChanging) {
        switch (tabController.index) {
          // type 类型 0全部 1代付款 2待收货 3取消 4已完成 5未评价 6已评价 7售后 8售后记录 9待发货
          case 0:
            type = 0;
            break;
          case 1:
            type = 1;
            break;
          case 2:
            type = 9;
            break;
          case 3:
            type = 2;
            break;
          case 4:
            type = 3;
            break;
          case 5:
            type = 4;
            break;
        }

        page = 1;
        getMyorderLists();
      }
    });
    super.onInit();
  }

//获取我的订单列表
  getMyorderLists() {
    Tools.showLoading();
    Network.getMyOrderList(type, page, 10, orderType: 5, keyword: keyWords).then((value) {
      if (page == 1) {
        orderList = value['list'];
      } else {
        orderList.addAll(value['list']);
      }
      hasNextPage = value['hasNextPage'];
      update();
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  receive(String orderId) {
    Tools.showLoading();
    Network.receive(orderId).then((value) {
      if (value) {
        Tools.showToast("收货成功");
        EventMgr.send(EventType.refreshOrderList);
        EventMgr.send(EventType.refreshLogin);
      }
      Tools.hideLoading();
    }).catchError((error) {
      Tools.hideLoading();
    });
  }

  void cancelOrder(String orderId) {
    Tools.showModal(
        content: "确定要取消该订单么?",
        onCancel: (tag) => {},
        onConfirm: (tag) {
          Tools.showLoading();
          Network.cancelOrder(orderId).then((value) {
            if (value) {
              SmartDialog.dismiss(tag: tag);
              Tools.showToast("已取消");
              onPageRefresh();
              update();
            }
            Tools.hideLoading();
          }).catchError((error) {
            Tools.hideLoading();
          });
        });
  }

  @override
  void onPageRefresh() async {
    super.onPageRefresh();
    page = 1;
    getMyorderLists();
    baseRefreshCtr.refreshCompleted(resetFooterState: true);
  }

  @override
  void onPageLoading() async {
    if (hasNextPage == true) {
      super.onPageLoading();
      page += 1;
      baseRefreshCtr.loadComplete();
    }
  }

  @override
  void onClose() {
    tabController.dispose();
    super.onClose();
  }

  //搜索
  void search(String keyword) {
    page = 1;
    // type = 0;
    keyWords = keyword;
    getMyorderLists();
    update();
  }
}
